﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities;

namespace Data
{
    public class ThamSoRepository
    {
        public static ThamSoDTO GetThamSoByTenThamSo(string tenThamSo)
        {
            NhaSachEntities context = new NhaSachEntities();
            var q = (from ts in context.ThamSoList
                    where ts.TenThamSo == tenThamSo
                    select new ThamSoDTO()
                    {
                        MaThamSo = (int)ts.MaThamSo,
                        GiaTri = (double)ts.GiaTri,
                        QuiDinh = (int)ts.QuiDinh,
                        SuDung=(int)ts.SuDung
                    }).FirstOrDefault();
            return q;
        }
        public static ThamSoDTO GetThamSoByMaThamSo(int maThamSo)
        {
            NhaSachEntities context = new NhaSachEntities();
            var q = (from ts in context.ThamSoList
                     where ts.MaThamSo == maThamSo
                     select new ThamSoDTO {
                         MaThamSo = (int)ts.MaThamSo,
                         GiaTri = (double)ts.GiaTri,
                         QuiDinh = (int)ts.QuiDinh,
                         SuDung = (int)ts.SuDung
                     }).FirstOrDefault();
            return q;
        }
        public static List<ThamSoDTO> GetThamSoList()
        {
            NhaSachEntities context = new NhaSachEntities();
            var q = (from ts in context.ThamSoList
                     select new ThamSoDTO()
                     {
                         MaThamSo=ts.MaThamSo,
                         TenThamSo=ts.TenThamSo,
                         GiaTri=(double)ts.GiaTri,
                         SuDung=(int)ts.SuDung,
                         QuiDinh=(int)ts.QuiDinh

                     }).DefaultIfEmpty();
            return q.ToList();
        }
        public static void Update(ThamSoDTO thamSo)
        {
            NhaSachEntities context=new NhaSachEntities();
            var q = (from ts in context.ThamSoList
                     where ts.MaThamSo == thamSo.MaThamSo
                     select ts).SingleOrDefault();
            q.GiaTri = thamSo.GiaTri;
            q.SuDung = thamSo.SuDung;
            context.SaveChanges();                
        }
    }
}
